/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import Enum.LoaiNguoiDungEnum;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.NguoiDung;
import pojo.SanPhamCuaHang;
import util.NewHibernateUtil;

/**
 *
 * @author 0
 */
public class SanPhamCuaHangDAO extends BaseDAO{
    public static List<SanPhamCuaHang> TimKiem(Integer maSanPhamCuaHang,String tenSanPham,Integer idCuaHang,Integer idLoaiSanPham,Double donGiaFrom,Double donGiaTo,NguoiDung nguoiDung,boolean forDialogSearch) throws Exception{
        Session session=NewHibernateUtil.getSessionFactory().openSession();
        String string="from SanPhamCuaHang spch where (spch.isDelete=false or spch.isDelete=null) and (spch.sanPham.isDelete=false or spch.sanPham.isDelete=null)";
        if (nguoiDung.getLoaiNguoiDung().getId() != LoaiNguoiDungEnum.Admin.getCode() || forDialogSearch) {
            string += String.format(" and spch.cuaHang.id=%d", nguoiDung.getCuaHang().getId());
        }
        if(maSanPhamCuaHang!=null){
            string+=String.format(" and spch.id=%d", maSanPhamCuaHang);
        }
        if(!tenSanPham.isEmpty()){
            string+=" and lower(spch.sanPham.tenSanPham) like :tenSanPham";
        }
        if(idCuaHang!=null){
            string+=String.format(" and spch.cuaHang=%d", idCuaHang);
        }
        if(idLoaiSanPham!=null){
            string+=String.format(" and spch.sanPham.loaiSanPham.id=%d", idLoaiSanPham);
        }
        if(donGiaFrom!=null){
            string+=String.format(" and spch.sanPham.donGia>=%f", donGiaFrom);
        }
        if(donGiaTo!=null){
            string+=String.format(" and spch.sanPham.donGia<=%f", donGiaTo);
        }
        Query query=session.createQuery(string);
        if(!tenSanPham.isEmpty()){
            query.setString("tenSanPham", "%"+tenSanPham.toLowerCase()+"%");
        }
        List<SanPhamCuaHang> sanPhamCuaHangs=query.list();
        session.close();
        return sanPhamCuaHangs;
    }
    
    public static SanPhamCuaHang LayTheoID(Integer id,NguoiDung nguoiDung) throws Exception{
        Session session=NewHibernateUtil.getSessionFactory().openSession();
        String string=String.format("from SanPhamCuaHang spch where (spch.isDelete=false or spch.isDelete=null) and (spch.sanPham.isDelete=false or spch.sanPham.isDelete=null) and spch.id=%d",id);
        if(nguoiDung.getLoaiNguoiDung().getId()!=LoaiNguoiDungEnum.Admin.getCode()){
            string+=String.format(" and spch.cuaHang.id=%d",nguoiDung.getCuaHang().getId());
        }
        SanPhamCuaHang sanPhamCuaHang=(SanPhamCuaHang) session.createQuery(string).uniqueResult();
        session.close();
        return sanPhamCuaHang;
    }
    
    public static int Them(SanPhamCuaHang sanPhamCuaHang) throws Exception{
        int id=0;
        Session session=NewHibernateUtil.getSessionFactory().openSession();
        Transaction transaction=session.beginTransaction();
        try{
            SanPhamCuaHang spch=(SanPhamCuaHang) session.createQuery(String.format("from SanPhamCuaHang spch where (spch.isDelete=false or spch.isDelete=null) and spch.sanPham.id=%d",sanPhamCuaHang.getSanPham().getId())).uniqueResult();
            if(spch!=null){
                session.close();
                throw new Exception("Sản phẩm đã tồn tại trong cửa hàng");
            }
            id=(int) session.save(sanPhamCuaHang);
            transaction.commit();
            session.close();
        }
        catch(HibernateException exception){
            transaction.rollback();
            session.close();
            throw new Exception(exception.getMessage());
        }
        return id;
    }
    
    public static List<SanPhamCuaHang> LayDanhSachBaoCaoTon(Integer idCuaHang) throws Exception{
        Session session=NewHibernateUtil.getSessionFactory().openSession();
        List<SanPhamCuaHang> sanPhamCuaHangs=session.createQuery(String.format("from SanPhamCuaHang spch where spch.cuaHang.id=%d order by spch.id",idCuaHang)).list();
        session.close();
        return sanPhamCuaHangs;
    }
}
